<?php
namespace App\Controller;
//namespace App\Service\ProfileService;
use App\Service\UploadService;
use App\Service\UserService;
use Swoole;
use App\Service\LoginService;
use App\DAO;
use App\Libs\Log;
class user extends  Base
{
    function __construct(\Swoole $swoole)
    {
        //$this->log->info("hahahaah");
        //Log::info("fuck");
        //Swoole::getInstance()->beforeAction($this->checkhead());

        parent::__construct($swoole);

    }
    function login()
    {

        $plat = $_GET['plat'];

        $code = $_GET['code'];
        $openid = $_GET['openid'];
        $accesstoken = $_GET['accesstoken'];

        $userService = new UserService($plat);
        if($plat == 1  ) {
            $ret = $userService->loginwithcode($code);
        }
        else if($plat == 0) {
            $ret = $userService->loginWithAccessToken($openid, $accesstoken);
        }

        return json_encode($ret);
        //$ret = $profileservice->getUserPorfile($account_list,$tag_list);
        //var_dump($ret);
    }
    function  upload()
    {
        $upload = new  UploadService();
        $ret =  $upload->upload();
        return json_encode($ret);
    }
    function update()
    {
        $usernick = isset($_GET['usernick']) ? $_GET['usernick']:null;
        $usersex = isset($_GET['sex']) ? $_GET['sex'] : null ;
        $userage = isset($_GET['age']) ? $_GET['age'] : null ;
        $userimg = isset($_GET['headimg']) ? $_GET['headimg'] : null ;
        $userinfo = array();
        if(!empty($usernick))
            $userinfo['user_nick']  = $usernick;
        if($usersex != NULL )
            $userinfo['user_sex'] = $usersex;
        if(!empty($userage))
            $userinfo['user_age'] = $userage;
        if(!empty($userimg))
            $userinfo['user_head_img'] = $userimg;


        if($userinfo)
        {
            $userservice = new UserService();
            $retuserinfo = $userservice->updateUserInfo($this->userinfo['user_id'],$this->token,$userinfo);

        }
        $ret = array(
            "errorcode" =>'0',
            'errmsg'=>"修改成功",
            'userinfo'=>$retuserinfo
        );

        return json_encode($ret);

    }
    function get()
    {
        $userService = new UserService();
        $userid = $_GET['userid'];

        $userinfo =  $userService ->getUserInfobyUserId($userid);
        $ret = array(
            'errorcode'=>'0',
            'errmsg' => '成功',
            'userinfo' => $userinfo
        );
        return json_encode($ret);

    }

    function test()
    {
        $userDao = new DAO\UserDAO();
        var_dump($userDao->getone());
    }

}